home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Arsenal Files 8
/
The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO
/
prg_casm
/
snpd9611.zip
/
ENVIRON.TXT
< prev
next >
Wrap
Text File
|
1996-11-24
|
3KB
|
58 lines
.I 0 56
+++Date last modified: 29-Oct-1996
Q. Is it possible to set an environment variable from within a program? I've
tried setnev() and it doesn't work.
A. Generally, the answer is no. First of all, you must understand how
environment variables work. The command shell (COMMAND.COM, 4DOS, or any
of the Unix shells) is what maintains the environment variables. They are
stored, usually concatenated, in a block of memory owned by the command
shell. When your program runs, it inherits a copy of the environment
block. This is a crucial concept! The command shell only copies its
"master environment" and passes the copy to the program. When the program
terminates, its copy, which was in memory it owned, is discarded along
with any changes which were made to it. This is why putenv() seems not to
work, since it only modifies the copy and the changes are lost upon
program termination. Ultimately, the question you must answer is which
copy of the environment you wish to modify? If you only wish to modify
your local environment, use putenv(). If you want to modify something
else, you have more questions to answer. Remember that several copies of
the command shell may be running at a time, so which one's environment
block do you want to modify? If you choose to modify your parent process'
environment block, that too will be lost when your parent process
terminates. If you want to modify the original master, then your changes
may still be lost when you terminate since your parent process may not be
the "master" command shell (see below for an illustration).
It's a can of worms that the makers of most operating systems (OS's),
including DOS, Unix, OS/2, Win95, et al, have wisely chosen to leave
sealed!
Q. What good is putenv() if its changes are lost when the program ends?
A. Its primary use is to set up environment variables prior to spawning
another subordinate process (program). In the PC world, you'd use it prior
to issuing a spawn call. In the Unix world, use it prior to using fork()
and exec().
Q. I know I've seen DOS programs which do modify the master environment, so I
know it can be done!
A. Yes it can, but not legally or with guaranteed results. Most OS's do have
undocumented ways of doing it. DOS is probably the safest to try since
it's not multiuser or multitasking, and so the only person you can screw
up is yourself! There are 4 ways to modify a non-local environment under
DOS. Only one is marginally legal and the others rely on undocumented DOS
features.